package com.code.google.mdsc.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

import com.code.google.mdsc.control.Business;
import com.code.google.mdsc.gui.i18n.Messages;
import com.code.google.mdsc.gui.table.HighlightRenderer;

/**
 *
 */
public class MainWindow {

	private final JFrame mainFrame;
	private final Business business;
	private final JTable table;
	private final JProgressBar progressBar;

	private static final Object[] COLUMN_HEADER_VALUES = {
			Messages.getString("COLUMN_MANDATE"),
			Messages.getString("COLUMN_ACCOUNT_HOLDER"),
			Messages.getString("COLUMN_BOOKING_VOLUMNE"),
			Messages.getString("COLUMN_MANDATORY_SIGNATURE_DATE"),
			Messages.getString("COLUMN_PURPOSE"),
			Messages.getString("COLUMN_IBAN"),
			Messages.getString("COLUMN_BIC"),
			Messages.getString("COLUMN_DATE_OF_BOOKING"),
			Messages.getString("COLUMN_SEQUENCE_TYPE") };

	public MainWindow(Business business) {

		super();
		this.business = business;
		this.mainFrame = new JFrame(Messages.getString("PROGRAM_TITLE"));
		this.progressBar = new JProgressBar(0, 100);
		this.progressBar.setStringPainted(true);

		JMenuBar menubar = buildMenuBar();
		menubar.setBackground(Color.WHITE);
		table = createTable();
		Container c = mainFrame.getContentPane();

		JPanel header = new JPanel();
		header.setLayout(new BorderLayout());
		ImagePanel ip = new ImagePanel("img/header.jpg");

		header.add(ip, BorderLayout.NORTH);

		header.add(menubar, BorderLayout.SOUTH);

		c.setLayout(new BorderLayout());
		c.add(header, BorderLayout.NORTH);
		c.add(new JScrollPane(table), BorderLayout.CENTER);
		c.add(progressBar, BorderLayout.SOUTH);
		mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		mainFrame.setVisible(true);
	}

	private JMenuBar buildMenuBar() {
		JMenuBar menubar = new JMenuBar();

		JMenu option = new JMenu(Messages.getString("MENU_OPTIONS"));
		JMenu optionsConfigure = new JMenu(Messages.getString("MENU_CONFIGURE"));
		final JMenuItem optionsConfigureCreditor = new JMenuItem(
				Messages.getString("MENU_CREDITOR"));
		optionsConfigureCreditor.setEnabled(false);
		JMenu data = new JMenu(Messages.getString("MENU_DATA"));
		final JMenuItem dataLoadCSV = new JMenuItem(
				Messages.getString("MENU_LOAD_CSV"));

		final JMenuItem dataSaveXML = new JMenuItem(
				Messages.getString("MENU_SAVE_XML"));

		dataSaveXML.setEnabled(false);
		dataLoadCSV.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				int returnValue = chooser.showOpenDialog(null);

				if (returnValue == JFileChooser.APPROVE_OPTION) {
					business.loadFromCSV(chooser.getSelectedFile(),
							MainWindow.this);
					dataLoadCSV.setEnabled(false);
					dataSaveXML.setEnabled(true);
					optionsConfigureCreditor.setEnabled(true);
				}

			}
		});

		dataSaveXML.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				int returnValue = chooser.showSaveDialog(null);

				if (returnValue == JFileChooser.APPROVE_OPTION) {
					business.saveAsXML(chooser.getSelectedFile(),
							MainWindow.this);
					dataSaveXML.setEnabled(false);
					dataLoadCSV.setEnabled(true);
				}

			}
		});
		JMenuItem dataExit = new JMenuItem(Messages.getString("MENU_EXIT"));

		dataExit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				business.exit();
			}
		});

		data.add(dataLoadCSV);
		data.add(dataSaveXML);
		data.addSeparator();
		data.add(dataExit);

		menubar.add(data);

		optionsConfigureCreditor.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new CreditorWindow(business, MainWindow.this);
			}
		});
		optionsConfigure.add(optionsConfigureCreditor);

		JMenuItem optionsSearch = new JMenuItem(
				Messages.getString("MENU_SEARCH"));

		optionsSearch.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new SearchWindow(business, MainWindow.this);
			}
		});
		JMenuItem optionsHelp = new JMenuItem(Messages.getString("MENU_HELP"));

		optionsHelp.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(mainFrame,
						Messages.getString("MENU_HELP_ENTRY"));
			}
		});

		option.add(optionsConfigure);
		option.add(optionsSearch);
		option.addSeparator();
		option.add(optionsHelp);
		menubar.add(option);
		return menubar;
	}

	private JTable createTable() {

		Object[][] o = new Object[0][COLUMN_HEADER_VALUES.length];

		DefaultTableModel dtm = new DefaultTableModel(o, COLUMN_HEADER_VALUES);
		final JTable j = new JTable(dtm);

		j.setAutoCreateRowSorter(true);

		return j;
	}

	public JFrame getMainFrame() {
		return mainFrame;
	};

	public JProgressBar getProgressBar() {
		return progressBar;
	}

	public JTable getTable() {
		return table;
	}

	public void highlightSearchedValueInTable(int row) {
		table.scrollRectToVisible(table.getCellRect(row, 0, true));

		table.setRowSelectionInterval(row, row);

		for (int i = 0; i <= table.getColumnCount() - 1; i++) {

			table.getColumnModel().getColumn(i)
					.setCellRenderer(new HighlightRenderer());
		}
	}

}
